//
// Created by 32827 on 2023/12/6.
//
#include "yzl_spi_sd.h"
#include "yz_includes.h"
//下面是MicroSD的初始化及读写函数
void YZL_SD_SPI_ReadWriteByteStart(YZF_SPI_POINTER spi){
#if YZL_SD_SPI_Use_Softport
    yzl_softport_spi_start(spi);
#else
    yz_spi_recive_transmit_full_lines_start(spi);
#endif
}
char YZL_SD_SPI_ReadWriteByte(YZF_SPI_POINTER spi,char data){
#if YZL_SD_SPI_Use_Softport
    return yzl_softport_spi_transmit_byte8(spi,data);
#else
    return yz_spi_recive_transmit_full_lines(spi,data);
#endif
}
void YZL_SD_SPI_ReadWriteByteEnd(YZF_SPI_POINTER spi){
#if YZL_SD_SPI_Use_Softport
    yzl_softport_spi_end(spi);
#else
    yz_spi_recive_transmit_full_lines_end(spi);
#endif
}
///---------------- 回复类型处理 ----------------

uint8_t sd_result[32];
uint8_t *sd_response_0(YZF_SPI_POINTER spi,char cmd){
    uint16_t times=0;
    do{  //读取后8位
        sd_result[0] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        times++;
    }
    while((sd_result[0]==0xff)&&(times<50));
    return sd_result;
}
uint8_t *sd_response_1(YZF_SPI_POINTER spi,char cmd){
    uint16_t times=0;
    do{
        sd_result[0] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        times++;
    }while((sd_result[0]&0b10000000)&&(times<50));
    return sd_result;
}
uint8_t *sd_response_2(YZF_SPI_POINTER spi,char cmd){
    uint16_t times=0;
    do{
        sd_result[0] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        times++;
    }while((sd_result[0]&0b10000000)&&(times<50));
    for(uint8_t i=1;i<2;i++){
        sd_result[i] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
    }
    return sd_result;
}
uint8_t *sd_response_3(YZF_SPI_POINTER spi,char cmd){
    uint16_t times=0;
    do{
        sd_result[0] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        times++;
    }while((sd_result[0]&0b10000000)&&(times<50));
    for(uint8_t i=1;i<5;i++){
        sd_result[i] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
    }
    return sd_result;
}
uint8_t *sd_response_6(YZF_SPI_POINTER spi,char cmd){
    uint16_t times=0;
    do{
        sd_result[0] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        times++;
    }while(sd_result[0]!=(cmd&0b00111111)&&(times<50));
    for(uint8_t i=1;i<5;i++){
        sd_result[i] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
    }
    return sd_result;
}
uint8_t *sd_response_7(YZF_SPI_POINTER spi,char cmd){
    uint16_t times=0;
    do{
        sd_result[0] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        times++;
    }while((sd_result[0]&0b10000000)&&(times<50));
    for(uint8_t i=1;i<5;i++){
        sd_result[i] = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
    }
    return sd_result;
}
///---------------- 回复类型处理 ----------------
YZF_RECV MicroSDData={0};
uint8_t sd_response_data_token(YZF_SPI_POINTER spi){
    uint8_t data;
    //起始符读取
    do{
        data = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
    } while ((data&(0b00010001))!=0b00000001);
    return data;
}
uint32_t sd_write_wait(YZF_SPI_POINTER spi){
    uint32_t trys=0;
    uint8_t data;
    do{
        data = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        trys++;
    } while ((data==0x00)&&(trys<10000));
    return trys;
}
uint8_t sd_response_block_read_raw(YZF_SPI_POINTER spi,char cmd,uint16_t length,uint8_t *area){
    uint16_t times=0;

    uint8_t data;
    //起始符读取
    do{
        data = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        times++;
        if(times>1000){
            return YZF_ERROR;
        }
    }while(data!=(MicroSD_READ_TOKEN));

    //数据读取
    for(uint16_t i=0;i<length;i++){
       area[i]= YZL_SD_SPI_ReadWriteByte(spi,0xFF);
    }

    //crc位
    sd_result[1]= YZL_SD_SPI_ReadWriteByte(spi,0xFF);
    sd_result[2]= YZL_SD_SPI_ReadWriteByte(spi,0xFF);

    return YZF_OK;
}
uint8_t * sd_response_block_read(YZF_SPI_POINTER spi,char cmd,uint16_t length,YZF_RECV *area){
    uint16_t times=0;

    uint8_t data;
    //起始符读取
    do{
        data = YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        times++;
    }while(data!=(MicroSD_READ_TOKEN));

    //数据读取
    for(uint16_t i=0;i<length;i++){
        yzf_area_recv_add(area, YZL_SD_SPI_ReadWriteByte(spi,0xFF));
    }

    //crc位
    sd_result[1]= YZL_SD_SPI_ReadWriteByte(spi,0xFF);
    sd_result[2]= YZL_SD_SPI_ReadWriteByte(spi,0xFF);

    return yzf_area_recv_get_read_ptr(area);
}
//写处理
uint8_t sd_response_block_write_raw(YZF_SPI_POINTER spi,char cmd,uint16_t length,uint8_t *area,uint16_t count){
    uint8_t res=0;
    if(cmd==0x58){
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        //发送起始符
        YZL_SD_SPI_ReadWriteByte(spi,MicroSD_SINGLE_WRITE_TOKEN);
        //数据读取
        for(uint16_t i=0;i<length;i++){
            YZL_SD_SPI_ReadWriteByte(spi,area[i]);
        }

        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        res=sd_response_data_token(spi);
        sd_write_wait(spi);
    }else if(cmd==0x59){
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);

        for(uint16_t i=0;i<count;i++){
            YZL_SD_SPI_ReadWriteByte(spi,MicroSD_MULTIPLE_WRITE_TOKEN_START);
            for(uint16_t j=0;j<length;j++){
                YZL_SD_SPI_ReadWriteByte(spi,area[i * 512 + j]);
            }
            res=sd_response_data_token(spi);
            if((res&0b00011111)!=0b00000101){
                return res;
            }
            sd_write_wait(spi);
        }
        YZL_SD_SPI_ReadWriteByte(spi,MicroSD_MULTIPLE_WRITE_TOKEN_END);
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        sd_write_wait(spi);
    }
    return res;
}
//写处理
uint8_t sd_response_block_write(YZF_SPI_POINTER spi,char cmd,uint16_t length,YZF_RECV *area,uint16_t count){
    uint8_t res=0;
    if(cmd==0x58){
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        //发送起始符
        YZL_SD_SPI_ReadWriteByte(spi,MicroSD_SINGLE_WRITE_TOKEN);
        //数据读取
        for(uint16_t i=0;i<length;i++){
            YZL_SD_SPI_ReadWriteByte(spi,yzf_area_recv_get(area));
        }

        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        res=sd_response_data_token(spi);
        sd_write_wait(spi);
    }else if(cmd==0x59){
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);

        for(uint16_t i=0;i<count;i++){
            YZL_SD_SPI_ReadWriteByte(spi,MicroSD_MULTIPLE_WRITE_TOKEN_START);
            for(uint16_t j=0;j<length;j++){
                YZL_SD_SPI_ReadWriteByte(spi,yzf_area_recv_get(area));
            }
            res=sd_response_data_token(spi);
            if((res&0b00011111)!=0b00000101){
                return res;
            }
            sd_write_wait(spi);
        }
        YZL_SD_SPI_ReadWriteByte(spi,MicroSD_MULTIPLE_WRITE_TOKEN_END);
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        YZL_SD_SPI_ReadWriteByte(spi,0xFF);
        sd_write_wait(spi);
    }
    return res;
}
uint8_t *SentCommandToMicroSDV2(YZF_SPI_POINTER spi,char cmd,uint32_t data,uint8_t crc,enum SD_Response res_type){
    YZL_SD_SPI_ReadWriteByteStart(spi);

    char times;
    MicroSD_CS_SET;//禁止SD卡片选 同步时钟
    YZL_SD_SPI_ReadWriteByte(spi,0xff);
    MicroSD_CS_RESET;//开始传输

    YZL_SD_SPI_ReadWriteByte(spi,cmd | 0b01000000);
    for(times=0;times<4;times++)
    {
        YZL_SD_SPI_ReadWriteByte(spi,(data >> 24) & 0xff);
        data<<=8;
    }

    YZL_SD_SPI_ReadWriteByte(spi,(crc << 1) + 1);
    uint8_t *res;
    //数据传输
    switch (res_type) {
        case SD_Response_0:
            res=sd_response_0(spi,cmd);
            break;
        case SD_Response_1:
            res=sd_response_1(spi,cmd);
            break;
        case SD_Response_2:
            res=sd_response_2(spi,cmd);
            break;
        case SD_Response_3:
            res=sd_response_3(spi,cmd);
            break;
        case SD_Response_6:
            res=sd_response_6(spi,cmd);
            break;
        case SD_Response_7:
            res=sd_response_7(spi,cmd);
            break;
        case SD_Response_NONE:
            res=sd_response_1(spi,cmd);
            return res;
        default:
            res=YZF_NULL_PTR;
    }

    YZL_SD_SPI_ReadWriteByteEnd(spi);
    MicroSD_CS_SET;//禁止SD卡片选 同步时钟
    return res;
}
uint8_t *yz_sd_get_result(){
    return sd_result;
}
YZF_RESULT yz_sd_spi_cmd0(YZF_SPI_POINTER spi){
    int retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x40,0,0x4A,SD_Response_0);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    }
    while(sd_result[0]!=0x01);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd8(YZF_SPI_POINTER spi){
    int retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x48,0x0000010E,0x14,SD_Response_7);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    }
    while (sd_result[0]!=0x01);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd9(YZF_SPI_POINTER spi){
    int retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x49,0x00000000,0x57,SD_Response_NONE);
        retry++;
        if(retry>800)
        {
            YZL_SD_SPI_ReadWriteByteEnd(spi);
            return YZF_ERROR;
        }
    }
    while (sd_result[0]!=0x01);
    sd_response_block_read(spi,0x49,16,&MicroSDData);
    YZL_SD_SPI_ReadWriteByteEnd(spi);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd10(YZF_SPI_POINTER spi){
    int retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x4a,0x00000000,0x0D,SD_Response_NONE);
        retry++;
        if(retry>800)
        {
            YZL_SD_SPI_ReadWriteByteEnd(spi);
            return YZF_ERROR;
        }
    }
    while (sd_result[0]!=0x01);
    sd_response_block_read(spi,0x4a,16,&MicroSDData);
    YZL_SD_SPI_ReadWriteByteEnd(spi);
    return YZF_OK;
}
//STOP Transmit
YZF_RESULT yz_sd_spi_cmd12(YZF_SPI_POINTER spi){
    int retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x4c,0x00000000,0x30,SD_Response_1);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    }
    while (sd_result[0]!=0x01);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd13(YZF_SPI_POINTER spi){
    int retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x4d,0x00000000,0x06,SD_Response_2);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    }
    while (sd_result[0]!=0x01);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd16(YZF_SPI_POINTER spi,uint32_t len){
    int retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x50,len,0x01,SD_Response_1);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    }
    while (sd_result[0]!=0x01);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd17(YZF_SPI_POINTER spi,uint32_t sector,YZF_RECV *area){
    uint16_t retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x51,sector,0xff,SD_Response_NONE);
        if(sd_result[0]==0x01){
            sd_response_block_read(spi,0x51,512,area);
        }
        retry++;
        if(retry>800)
        {
            YZL_SD_SPI_ReadWriteByteEnd(spi);
            return YZF_ERROR;
        }
    } while (sd_result[0]!=0x01);
    YZL_SD_SPI_ReadWriteByteEnd(spi);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd18_raw(YZF_SPI_POINTER spi,uint32_t sector,uint8_t *area,uint16_t len){
    uint16_t retry=0;
    SentCommandToMicroSDV2(spi,0x52,sector,0xff,SD_Response_NONE);
    while (sd_result[0]!=0x01){
        retry++;
        if(retry>200)
        {
            YZL_SD_SPI_ReadWriteByteEnd(spi);
            return YZF_ERROR;
        }
        YZL_SD_SPI_ReadWriteByteEnd(spi);
        SentCommandToMicroSDV2(spi,0x52,sector,0xff,SD_Response_NONE);
    }

    for(uint16_t i=0;i<len;i++){
        YZF_RESULT re=sd_response_block_read_raw(spi,0x51,512,&(area[512*i]));
        if(re!=YZF_OK){
            YZL_SD_SPI_ReadWriteByteEnd(spi);
            return re;
        }
    }
    yz_sd_spi_cmd12(spi);

    YZL_SD_SPI_ReadWriteByteEnd(spi);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd18(YZF_SPI_POINTER spi,uint32_t sector,YZF_RECV *area,uint16_t len){
    uint16_t retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x52,sector,0xff,SD_Response_NONE);
        if(sd_result[0]==0x01){
            for(uint16_t i=0;i<len;i++){
                sd_response_block_read(spi,0x51,512,area);
            }
            yz_sd_spi_cmd12(spi);
        }
        retry++;
        if(retry>800)
        {
            YZL_SD_SPI_ReadWriteByteEnd(spi);
            return YZF_ERROR;
        }
    } while (sd_result[0]!=0x01);
    YZL_SD_SPI_ReadWriteByteEnd(spi);
    return YZF_OK;
}


uint8_t YZL_SD_SPI_WriteResponse=0;
//单块写
YZF_RESULT yz_sd_spi_cmd24(YZF_SPI_POINTER spi,uint32_t sector,YZF_RECV *area){
    uint16_t retry=0;
    uint16_t data=0;
    do{
        SentCommandToMicroSDV2(spi,0x58,sector,0xff,SD_Response_NONE);
        if(sd_result[0]==0x01){
            data=sd_response_block_write(spi,0x58,512,area,1);
        }
        retry++;
        if(retry>800)
        {
            YZL_SD_SPI_ReadWriteByteEnd(spi);
            return YZF_ERROR;
        }
    } while (sd_result[0]!=0x01);
    YZL_SD_SPI_ReadWriteByteEnd(spi);
    if((data&0b00011111)!=0b00000101){
        return YZF_ERROR;
    }
    return YZF_OK;
}
//多块写
YZF_RESULT yz_sd_spi_cmd25(YZF_SPI_POINTER spi,uint32_t sector,YZF_RECV *area,uint16_t count){
    uint16_t retry=0;

    SentCommandToMicroSDV2(spi,0x59,sector,0xff,SD_Response_NONE);
    while (sd_result[0]!=0x01){
        YZL_SD_SPI_ReadWriteByteEnd(spi);
        SentCommandToMicroSDV2(spi,0x59,sector,0xff,SD_Response_NONE);
    }
    YZL_SD_SPI_WriteResponse=sd_response_block_write(spi,0x59,512,area,count);
    retry++;
    if(retry>800)
    {
        YZL_SD_SPI_ReadWriteByteEnd(spi);
        return YZF_ERROR;
    }
    //判断SD返回
    if((YZL_SD_SPI_WriteResponse&0b00011111)!=0b00000101){
        YZL_SD_SPI_ReadWriteByteEnd(spi);
        return YZF_ERROR;
    }
    YZL_SD_SPI_ReadWriteByteEnd(spi);
    return YZF_OK;
}
//多块写
YZF_RESULT yz_sd_spi_cmd25_raw(YZF_SPI_POINTER spi,uint32_t sector,uint8_t *area,uint16_t count){
    uint16_t retry=0;

    SentCommandToMicroSDV2(spi,0x59,sector,0xff,SD_Response_NONE);
    while (sd_result[0]!=0x01){
        retry++;
        if(retry>200)
        {
            YZL_SD_SPI_ReadWriteByteEnd(spi);
            return YZF_ERROR;
        }
        YZL_SD_SPI_ReadWriteByteEnd(spi);
        SentCommandToMicroSDV2(spi,0x59,sector,0xff,SD_Response_NONE);
    }
    YZL_SD_SPI_WriteResponse=sd_response_block_write_raw(spi,0x59,512,area,count);

    //判断SD返回
    if((YZL_SD_SPI_WriteResponse&0b00011111)!=0b00000101){
        YZL_SD_SPI_ReadWriteByteEnd(spi);
        return YZF_ERROR;
    }
    YZL_SD_SPI_ReadWriteByteEnd(spi);
    return YZF_OK;
}

YZF_RESULT yz_sd_spi_cmd55(YZF_SPI_POINTER spi){
    int retry=0;
    do
    {
        SentCommandToMicroSDV2(spi,0x77,0,0x32,SD_Response_1);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    }
    while(sd_result[0]!=0x01);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_acmd41(YZF_SPI_POINTER spi){
    int retry=0;
    do
    {
        SentCommandToMicroSDV2(spi,0x69,0x40000000,0x3B,SD_Response_3);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    } while (sd_result[0]==0xff);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd42(YZF_SPI_POINTER spi){
    int retry=0;
    do
    {
        SentCommandToMicroSDV2(spi,0x6a,0x00000000,0x28,SD_Response_1);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    }
    while(sd_result[0]!=0x01); //回应01h，停止写入
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd58(YZF_SPI_POINTER spi){
    //get OCR
    int retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x7a,0x00000000,0x7E,SD_Response_3);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    } while (sd_result[0]!=0x01);
    return YZF_OK;
}
YZF_RESULT yz_sd_spi_cmd59(YZF_SPI_POINTER spi){
    //off crc
    int retry=0;
    do{
        SentCommandToMicroSDV2(spi,0x7b,0x00000000,0x48,SD_Response_1);
        retry++;
        if(retry>800)
        {
            return YZF_ERROR;
        }
    } while (sd_result[0]!=0x01);
    return YZF_OK;
}

YZF_BOOL SD_isInit=YZF_FALSE;
//初始化化不是很稳定也就是说明传输数据可能不是很稳定
char yzl_sd_spi_init(YZF_SPI_POINTER spi)
{
    if(SD_isInit){
        return YZF_OK;
    }

    MicroSD_CS_SET;
    //发送至少74个时钟信号，数据顺便填写

    for (char i=0;i<0x2f;i++){ YZL_SD_SPI_ReadWriteByte(spi,0xff);}

    //CMD0
    if(yz_sd_spi_cmd0(spi)==YZF_ERROR){
        yz_error("CMD0 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }

    if(yz_sd_spi_cmd8(spi)==YZF_ERROR){
        yz_error("CMD8 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }

    do{
        if(yz_sd_spi_cmd55(spi)==YZF_ERROR){
            yz_error("CMD55 Error! CODE:%d",sd_result[0]);
            return YZF_ERROR;
        }
        if(yz_sd_spi_acmd41(spi)==YZF_ERROR){
            yz_error("ACMD41 Error! CODE:%d",sd_result[0]);
            return YZF_ERROR;
        }
    } while (sd_result[0]!=0x01);

    //off crc
    if(yz_sd_spi_cmd59(spi)==YZF_ERROR){
        yz_error("CMD59 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }
    //get ocr
    if(yz_sd_spi_cmd58(spi)==YZF_ERROR){
        yz_error("CMD58 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }
    ///Get SD Card's Information to MicroSDData {[0:127]:CID,[128:255]:CSD}
    //CID
    if(yz_sd_spi_cmd10(spi)==YZF_ERROR){
        yz_error("CMD10 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }

    //CSD
    if(yz_sd_spi_cmd9(spi)==YZF_ERROR){
        yz_error("CMD9 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }

    if(yz_sd_spi_cmd16(spi,512)==YZF_ERROR){
        yz_error("CMD16 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }

    MicroSD_CS_SET;  //片选无效
    SD_isInit=YZF_TRUE;
    return YZF_OK;
}

YZF_RESULT WriteSectorToMicroSD(YZF_SPI_POINTER spi,long addr,YZF_RECV *area)
{
    if(yz_sd_spi_cmd24(spi,addr,area)==YZF_ERROR){
        yz_error("CMD24 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }
    return 0;
}
YZF_RESULT WriteSectorsToMicroSD(YZF_SPI_POINTER spi,long addr,YZF_RECV *area,uint16_t len)
{
    if(yz_sd_spi_cmd25(spi,addr,area,len)==YZF_ERROR){
        yz_error("CMD25 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }
    return 0;
}
YZF_RESULT WriteSectorsToMicroSD_Raw(YZF_SPI_POINTER spi,long addr,uint8_t *area,uint16_t len){
    if(yz_sd_spi_cmd25_raw(spi,addr,area,len)==YZF_ERROR){
        yz_error("WriteSector Error! CODE:%d\n",sd_result[0]);
        return YZF_ERROR;
    }
    return 0;
}
YZF_RESULT ReadSectorsFromMicroSD_Raw(YZF_SPI_POINTER spi,long sector,uint8_t *area,uint16_t len){
    if(yz_sd_spi_cmd18_raw(spi,sector,area,len)==YZF_ERROR){
        yz_error("ReadSector Error! CODE:%d\n",sd_result[0]);
        return YZF_ERROR;
    }
    return YZF_OK;
}
YZF_RESULT ReadSectorsFromMicroSD(YZF_SPI_POINTER spi,long sector,YZF_RECV *area,uint16_t len){
    if(yz_sd_spi_cmd18(spi,sector,area,len)==YZF_ERROR){
        return YZF_ERROR;
    }
    return YZF_OK;
}
YZF_RESULT ReadSectorFromMicroSD(YZF_SPI_POINTER spi,long sector,YZF_RECV *area)
{
    if(yz_sd_spi_cmd17(spi,sector,area)==YZF_ERROR){
        yz_error("CMD17 Error! CODE:%d",sd_result[0]);
        return YZF_ERROR;
    }
    return YZF_OK;
}